package com.hfzy.ihk.web.linkcall.biz;

import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.facade.calldata.entity.CallHistoryText;
import org.json.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Administrator
 * @Date: 2019/3/25 18:33
 * @Description:
 */
@Component
public class CallDataBiz {

    Logger  logger=LoggerFactory.getLogger(CallDataBiz.class);

    @Value("${callData.http.url}")
    String callDataUrl;


    RestTemplate restTemplate=new RestTemplate();

    /**
     * http调用callData获取通话记录的内容详情
     * @param param
     * @return
     */
    public Map<String,Object> queryCallHistoryTextList(Map<String, Object> param)  {

        Map<String,Object> resultMap = null;
        try {
            resultMap = getForObject("callTaskHistory/queryCallHistoryTextList",param);
            if (resultMap==null){
                throw new RuntimeException();
            }
        } catch (Exception e) {
            logger.error("http请求查询callData通话内容失败");
            throw new RuntimeException();
        }

        return  resultMap;
    }


    /**
     * http调用callData获取通话记录
     * @param param
     * @return
     */
    public Map<String,Object> queryCallHistoryList(Map<String, Object> param) {

        Map<String,Object> resultMap = null;
        try {
            resultMap = getForObject("callTaskHistory/queryCallHistoryList",param);

            if (resultMap==null){
                throw new RuntimeException();
            }
        } catch (Exception e) {
            logger.error("http请求查询callData通话记录失败");
            throw new RuntimeException();
        }

        return resultMap;
    }

    /**
     * 根据map的条件查询CallHistoryTest
     * @param param
     * @return
     */
    public List<CallHistoryText> searchCallHistoryTextList(Map<String,Object> param){


        Map<String,Object> resultMap = getForObject("callTaskHistory/searchCallHistoryTextList", param);

        String jsonStr = (String)resultMap.get("data");

        List<CallHistoryText> callHistoryTexts = JSON.parseArray(jsonStr, CallHistoryText.class);

        return callHistoryTexts;
    }


    public  String callTaskCompletion(String callTaskId) {

        Map<String,Object> params=new HashMap<>();

        params.put("callTaskId",callTaskId);

        Map<String, Object> resultMap = getForObject("callTaskHistory/searchCallHistoryTextList", params);

        Object result = resultMap.get("result");

        return (String)result;
    }

    /**
     * 保存反馈操作
     * @param
     * @return
     */
    public String createOrUpdateCloudCallHistoryBySessionId(String saveData) {

        Map<String,Object> params=new HashMap<>();

        params.put("saveData",saveData);

        Map<String, Object> resultMap = getForObject("callTaskHistory/createOrUpdateCloudCallHistoryBySessionId", params);

        String result = (String)resultMap.get("result");

        return result;
    }


    public Map<String,Object> queryCallTaskDetailId(String callTaskId) {

        Map<String,Object> params=new HashMap<>();

        params.put("callTaskId",callTaskId);

        Map<String, Object> resultMap = getForObject("callTaskHistory/queryCallTaskDetailId", params);

        return resultMap;

    }

    /**
     * 用get请求调用callData服务
     * @param url
     * @param param
     * @return
     */
        public Map<String,Object> getForObject(String url,Map<String, Object> param){

        Map<String,Object> map=null;

        StringBuilder builder=new StringBuilder(callDataUrl);

        builder.append(url+"?");

        if (!param.isEmpty()){

                param.remove("access_token");

            for (String thekey : param.keySet()) {

                //builder.append(thekey+"="+param.get(thekey)+"&");
                builder.append(thekey+"={"+thekey+"}&");
            }

        }
        url= builder.delete(builder.length()-1,builder.length()).toString();

        map=restTemplate.getForObject(url,Map.class,param);

        return map;
    }

}
